import time
import asyncio
import requests
from bs4 import BeautifulSoup
import uuid
import schedule
from utils.HBaseConnect import HBaseConnect
import re
import json
from urllib import parse

from utils.MysqlConnect import MysqlConnect

url = 'https://www.kuaishou.com/brilliant'
tableName = "hot_search_keywords"

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36 Edg/125.0.0.0'
}


def save_hot_search_keywords(hbase, mysql, dic):
    print("排名" + dic['rank'], dic)
    hbase.putTable(tableName, rowKey=str(uuid.uuid1().hex), data={
        "info:keyword": dic['keyword'],
        "info:num": dic['searchNum'],
        "info:rank": dic['rank'],
        "info:url": dic['url'],
        "info:platform": "快手",
        "info:time": dic['time'],
    })
    insert_sql = (
        "INSERT INTO information(hot_keyword, search_num, ranking, url, platform,time) "
        "VALUES(%s, %s, %s, %s, %s, %s)")
    insert_args = (dic['keyword'], dic['searchNum'], dic['rank'], dic['url'], "快手", dic['time'])
    mysql.insert(insert_sql, insert_args)


async def get_hot_search_keywords():
    try:
        # 初始化hbase的参数
        hbase = HBaseConnect()
        hbase.start()
        mysql = MysqlConnect()
        current_time = time.strftime('%Y-%m-%d %H:%M:%S')
        dic = {}
        # 发送 HTTP GET 请求
        res = requests.get(url, headers=headers)
        # 解析 HTML 内容
        soup = BeautifulSoup(res.text, 'html.parser')

        # 提取脚本标记内容
        script_tag = soup.find('script', string=re.compile(r'window.__APOLLO_STATE__'))
        script_content = script_tag.string

        # 使用正则表达式从脚本内容中提取 JSON 数据
        json_data_match = re.search(r'window.__APOLLO_STATE__=(\{.*\});', script_content)
        if json_data_match:
            json_data_str = json_data_match.group(1)
            json_data = json.loads(json_data_str)

            # 遍历 JSON 数据以查找所需项目
            vision_hot_rank = json_data['defaultClient']['$ROOT_QUERY.visionHotRank({"page":"brilliant"})']['items']

            for item_id in vision_hot_rank:
                item = json_data['defaultClient'][item_id['id']]
                dic['keyword'] = item['name']
                # 去除'万'并将其转换为浮点数
                number_in_million = float(item['hotValue'].replace('万', ''))
                dic['searchNum'] = str(int(number_in_million * 10000))
                dic['rank'] = str(item['rank'])
                dic['time'] = current_time
                original_url = 'https://www.kuaishou.com/short-video/' + item['photoIds']['json'][
                    0] + '?streamSource=hotrank&trendingId=' + item['id'] + '&area=brilliantxxunknown'
                dic['url'] = parse.quote(original_url, safe=':/?=&')
                save_hot_search_keywords(hbase, mysql, dic)
        hbase.stop()
        # 关闭mysql
        mysql.cur.close()
        mysql.connection.close()
    except Exception as e:
        print("Exception", e)


def run_asyncio_task():
    print("当前时间", time.strftime('%Y-%m-%d %H:%M:%S'))
    asyncio.run(get_hot_search_keywords())


if __name__ == '__main__':
    # 在每小时的00, 10, 20, 30, 40, 50分钟运行任务
    schedule.every().hour.at(":00").do(run_asyncio_task)
    schedule.every().hour.at(":10").do(run_asyncio_task)
    schedule.every().hour.at(":20").do(run_asyncio_task)
    schedule.every().hour.at(":30").do(run_asyncio_task)
    schedule.every().hour.at(":40").do(run_asyncio_task)
    schedule.every().hour.at(":50").do(run_asyncio_task)
    while True:
        schedule.run_pending()
        time.sleep(1)
